// pages/register/register.js
const app = getApp()
import CryptoJS from '../../utils/aes/CryptoJS';
const Crypt = CryptoJS.Crypt
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: "请输入验证码",
    // 手机注册
    register: {
      telephone: '',
      captcha: '',
      password: '',
      parentUserId: '' //邀请码
    },
  },

  codeLogin() {
    if (!!!this.data.register.telephone || this.data.register.telephone.length < 11 || this.data.register.telephone.length > 11 ) {
      app.error("手机号格式错误!")
    } else if (!!this.data.register.telephone && !!this.data.register.captcha && !!this.data.register.password) {
      var postObj = {
        url: '/user/register',
        method: 'POST',
        data: {
          telephone: this.data.register.telephone,
          captcha: this.data.register.captcha,
          password: Crypt(this.data.register.password),
          parentUserId: '' //邀请码
        }
      }
      app.request.loginRequest(postObj).then(res => {
        console.log('手机注册res', res)
        app.success("注册成功")
        var userId = res.data.data.userId
        var token = res.data.data.token
        wx.setStorageSync('userId', userId)
        wx.setStorageSync('token', token)
        // wx.switchTab({
        //   url: '/pages/chatRoom/chatRoom',
        // })
        // wx.navigateBack({
        //   delta:3
        // })
        // wx.navigateTo({
        //   url: '/pages/my/setUserInfo/setUserInfo',
        // })
        wx.redirectTo({
          url: '/pages/my/setUserInfo/setUserInfo',
        })
        
      }).catch(err => {
        console.log('手机注册err', err)
        if (!!err.data.data) {
          app.error(app.util.forin(err.data.data))
        } else {
          app.error(err.data.desc)
        }
      })
    } else {
      app.error("不能为空!")
    }

  },

  // 获取短信验证码接口
  getCode() {
    if (this.data.time == "请输入验证码") {
      if (!!this.data.register.telephone && this.data.register.telephone.length == 11) {
        var postObj = {
          url: '/user/captcha',
          method: 'POST',
          data: {
            telephone: this.data.register.telephone,
            captchaType: "register"
          }
        }
        app.request.noLoginRequest(postObj).then(res => {
          console.log('短信验证码接口res', res)
          app.success("发送成功")
          this.start()
          console.log(res.data.data)
        }).catch(err => {
          console.log('短信验证码接口err', err)
        })
      } else {
        app.error("号码规格错误")
      }
    } else {
      app.error("请勿频繁操作！")
    }
  },
  start() {
    this.reset();
    const countDown = this.selectComponent('.control-count-down');
    countDown.start();

  },
  reset() {
    const countDown = this.selectComponent('.control-count-down');
    countDown.reset();
  },
  finished() {
    // this.reset();
    // this.setData({
    //   time: "请输入验证码"
    // })
  },
  onChangetime(e) {
    // console.log(e.detail)
    if (e.detail.seconds == 0 && e.detail.milliseconds == 0) {
      this.setData({
        time: "请输入验证码"
      })
    } else if (e.detail.seconds == 60 && e.detail.milliseconds == 0) {
      this.setData({
        time: "请输入验证码"
      })
    } else {
      this.setData({
        time: e.detail.seconds
      })
    }
  },
  // telephone
  onChangeRegisterTelephone(event) {
    var telephone = event.detail
    this.setData({
      [`register.telephone`]: telephone
    })
  },

  // captcha
  onChangeRegisterCaptcha(event) {
    var captcha = event.detail
    this.setData({
      [`register.captcha`]: captcha
    })
  },
  // password
  onChangeRegisterPassword(event) {
    var password = event.detail
    this.setData({
      [`register.password`]: password
    })
  },
  //parentUserId
  onChangeRegisterParentUserId(event) {
    var parentUserId = event.detail
    this.setData({
      [`register.parentUserId`]: parentUserId
    })
  },
  // 现在登陆
  loginNow() {
    wx.redirectTo({
      url: '/pages/login/login',
    })
  },
  // 暂不登陆
  loginNo() {
    // wx.switchTab({
    //   url: '/pages/chatRoom/chatRoom',
    // })
    wx.switchTab({
      url: '/pages/game/gameIndex/gameIndex',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})